package learn.io;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtil {

	/**
	 * 获取一个文件夹下的所有文件和子文件夹下的所有文件
	 * @param filePath
	 * @return
	 */
	public static List<File> readFiles(String filePath){
		if ("".equals(filePath)){
			return null;
		}

		File currentFile = new File(filePath);
		File[] files = currentFile.listFiles();

		List<File> fileList = new ArrayList<File>();

		for (File file : files){
			if (file.isDirectory()){
				readFile(file.getAbsolutePath());
			}else {
				fileList.add(file);
			}
		}

		for(File file : files) {
			System.out.println(file.getAbsolutePath());
		}
		return fileList;
	}

	/**
	 * 读取文件夹下的所有文件
	 * @param filepath
	 */
	public static void readFile(String filepath){
		File file = new File(filepath);
		File[] files = file.listFiles();
		List<File> fileList = new ArrayList<File>();
		for (File filet:files){
			fileList.add(filet);
		}

		for(File filett : fileList) {
			System.out.println(filett.getAbsolutePath());
		}
	}

	private static List<Tree> list = new ArrayList<>();//用来存放数据
	private static Integer id = 0;//因为测试使用，当初主键id来用
	public static void main(String[] args) {
//		String path = "C:\\Users\\PF0W8JF8\\Desktop\\上汽\\存证编号数据";
//		readFiles(path);


		Map<String,String> folder = new HashMap<>();
		folder.put("aa","文件夹1");
		folder.put("aa_bb","文件夹1-1");
		folder.put("aa_bb_cc","文件夹1-1-1");


		List<FileOO> fileOOS = new ArrayList<>();
		FileOO fileOO1 = new FileOO("aa","111111");
		FileOO fileOO2 = new FileOO("aa_bb","222222");
		FileOO fileOO3 = new FileOO("aa_bb_cc","3333333");
		fileOOS.add(fileOO1);
		fileOOS.add(fileOO2);
		fileOOS.add(fileOO3);


		for (int i=0;i<fileOOS.size();i++){
			FileOO fileOO = fileOOS.get(i);
			String folderId = fileOO.getFolderId();
			String fname = folder.get(folderId);

			String[] strings = folderId.split("_");
			String path = "";
			String mapKey = "";
			for (int j=0;j<strings.length;j++){
				String key = strings[j];
				if (j==0){
					mapKey = key;
					path = folder.get(mapKey);
				}else{
					mapKey = mapKey + "_"+key;
					path = path + "/"+folder.get(mapKey);
				}
			}
			System.out.println("文件层级："+strings.length+",文件路径："+path);
		}

	}

}
